<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Push Tester</title>
        <style>
            .qs-input{width:60px;}
            .subcode{width:120px;}
            label{margin-left:5px;}
            #divResult{width:100%;height:600px;overflow:auto;background:#fff;}
        </style>
        <script>
            var version={};
            version.push=window.location.protocol + "//pushdev.morningstar.com";
            var sessionID='RKS2GDU2AJY99JZFBUYI1V6768470110_1';
        </script>
        <script type="text/javascript" src="../lib/jquery.js"></script>
        <script type="text/javascript" src="../lib/jquery.json.min.js"></script>
        <script type="text/javascript" src="../lib/migratory-jslib.js"></script>
        <script type="text/javascript" src="../js/sys/dataManager.js"></script>
        <script>
            $(document).ready(function(){
                var count=1;
                var log=$('#divResult');
                var dataHandler=new QS.DataHandler('push');
                dataHandler.updateMarketPrice=function(body){
                    if(count==10){
                        log[0].innerHTML='';
                    }
                    var itemsReceived = body.items;
                    for (var i = 0, l = itemsReceived.length; i < l; i++) {
                        var sourceType = itemsReceived[i].source;// D or R.
                        var serviceName = itemsReceived[i].service;
                        var itemName = itemsReceived[i].symbol;
                        var fieldsForItem = itemsReceived[i].fields;
                        var info="sourceType:"+sourceType+", TenforeExchange:"+serviceName+", itemName:"+itemName;
                        var fieldsInfo=[];
                        for (var j = 0, len = fieldsForItem.length; j < len; j++) {
                            fieldsInfo.push(fieldsForItem[j].name+"="+fieldsForItem[j].value);
                        }
                        info+="{"+fieldsInfo.join(", ")+"}<br/>";
                        log.append(info);
                        count++;
                    }
                    log.append(body);
                }
                var dataObj=new QS.Push(dataHandler,{
                    onConnectedFailure:function(){
                        log.append("Connection failure!<br/>");
                    }
//                  onConnectedSuccess:function(){
//                      log.append("Connection Successful!<br/>");
//                  }
               });
               dataObj.initConnection(version.push,sessionID);
               
               var $subcodes=subCodes=$('#txtSubCodes');
               
               $('input[type="button"]').bind('click',function(e){
                     var source=$('#source').val();
                     var exch=$('#txtExch').val();
                     var ticker=$('#txtTicker').val();
                     var secType=$('#txtSecType').val();
                     var subCodes=$subcodes.val();
                     
                     if(exch==''||ticker==''||secType==''||subCodes==''){
                        alert("Required filds should not be empty!");
                     }else{
                        subCodes=subCodes.split(",");
                        var channels=[];
                        for(var i=0,l=subCodes.length;i<l;i++){
                            channels.push({
                                sourceType:source,
                                serviceName:exch,
                                itemName:secType+"|"+ticker+"|"+subCodes[i]
                             });
                        }
                        
                        if(channels.length>0){
                            if (this.id == 'btnSubscribe') {
                                dataObj.batchSubscribe(channels);
                            }else if(this.id == 'btnUnSubscribe'){
                                dataObj.batchUnsubscribe(channels);
                            }
                        }
                     }
               });
               
               var subcode=subCodes=$('#txtSubCodes');
               $('#source').bind('change',function(e){
                     if($(this).val()=='R'){
                        $subcodes.val('0,1,2,3,4,5,6,7,8,9,10');
                     }else{
                        $subcodes.val('-1');
                     }
               })
            });
        </script>
    </head>
    <body>
        <fieldset>
            <label>sourceType:</label>
            <select id="source">
                 <option value="R">Realtime</option>
                 <option value="D">Delay</option>   
            </select>
            <label>TenforeExchange:</label><input class="qs-input" id="txtExch" type="text" />
            <label>TenforeTicker:</label><input class="qs-input" id="txtTicker" type="text" />
            <label>SecurityType:</label><input class="qs-input" id="txtSecType" type="text" />
            <label>SubCodes:</label><input class="qs-input subcode" id="txtSubCodes" type="text" readonly=true value="0,1,2,3,4,5,6,7,8,9,10"/>
            <input id="btnSubscribe" type="button" value="Subscribe"/>
            <input id="btnUnSubscribe" type="button" value="UnSubscribe"/>
        </fieldset>
        <br/><br/><br/>
        <fieldset>
            <legend>Result</legend>
            <div id="divResult"></div>
        </fieldset>
    </body>
</html>
